package com.ifonly.activerecord.sql.handlers;

import com.ifonly.activerecord.Db;
import com.ifonly.activerecord.Model;
import com.ifonly.utils.ReflectUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-28 13:18
 * @since JDK 1.6
 */
public class ListModelResultSetHandler extends AbstractListHandler<Model> {
    private Db db = Db.getInstance();
    private Class<? extends Model> modelClass;
    private boolean defaultValue;

    public ListModelResultSetHandler(Class<? extends Model> modelClass) {
        this.modelClass = modelClass;
    }

    public ListModelResultSetHandler(Class<? extends Model> modelClass, boolean defaultValue) {
        this.modelClass = modelClass;
        this.defaultValue = defaultValue;
    }

    @Override
    public Model handleResultSet(ResultSet rs) throws SQLException {
        Model model = ReflectUtils.newInstance(modelClass);
        Map<String, Object> columnAndValues = rowResultSetToMap(rs);
        model.setAll(columnAndValues);
        return model;
    }

}
